import { Service, Inject } from 'typedi'
import { Entity } from '@/utils/orm'
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import { Menu } from '@/entity/Menu';
import { Form } from '@/entity/Form';
import { Article } from '@/entity/Article';
import { CommentParams, form,formlist } from '@/model/request/Comment';
@Service()
export class MenuDao {
    [x: string]: any;

    @Inject()
    orm!: Entity

    @Inject()
    json!: JsonResult<any>;


    async GetAllMenuDao(): Promise<res<Menu[]>> {
        let res = await this.orm.repository(Menu).getMany();
        let fa: Menu[] = []
        res.forEach(v => {
            if (v.menu_type == 1) {
                fa.push(<Menu>v)
                v.children = []
            }
        })

        res.forEach(j => {
            fa.forEach(v => {
                if (j.menu_father_id == v.menu_id) {
                    v.children.push(<Menu>j)
                }
            })
        })


        return this.json.success(fa);
    }

    async Push(params: formlist): Promise<res<null>> {
        await this.orm.source()
            .insert()
            .into(Article)
            .values([
                {
                    article_title: params.article_title,
                    article_text: params.article_text,
                    categories_id: params.categories_id.toString(),
                    tag_id: params.tag_id.toString(),
                    article_backimg: params.article_backimg
                    
                },
            ]) 
            .execute()
            console.log( "article_backimg",params.article_title);
            console.log( "article_backimg",params.article_text);
            console.log( "article_backimg",params.categories_id);
            console.log( "article_backimg",params.tag_id);
            console.log( "article_backimg",params.article_backimg);

        return this.json.success(null, msgList.pushMessageSuccess)
        
    }
}